<?php
namespace app\decorate\model;
use app\BaseModel;
use think\facade\Db;



class IndexCase extends BaseModel
{
    //定义表名 案列表
    protected $name = 'longbing_card_decorate_case';
    protected $append = [
        'area_text',
        'style_text',
        'housewear_text',
//        'desinger_text'
    ];
    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '=', $status);
    }

    /**
     * User: chenniang
     * Date: 2019-10-18 17:35
     * @param $value
     * @return void
     * descption:带单位
     */
    public function getMAttr($value){
        return $value.'㎡';
    }
    /**
     * User: chenniang
     * Date: 2019-10-18 17:35
     * @param $value
     * @return void
     * descption:带单位
     */
    public function getTotalPriceAttr($value){
        return $value.'万';
    }
    /**
     * User: chenniang
     * Date: 2019-10-15 11:51
     * @param $value
     * @param $data
     * @return mixed
     * descption:获取地区名字
     */
    public function getAreaTextAttr($value,$data){
        $area_model = new AdminArea();
        return $area_model->areaTitle(['id'=>$data['area']]);
    }

    /**
     * User: chenniang
     * Date: 2019-10-21 18:49
     * @param $value
     * @return void
     * descption:获取设计师详情
     */
    public function getDesignerAttr($value){
        $dis[] = ['a.fans_id','=',$value];
        $dis[] = ['a.is_staff','=',1];
        $user  = Db::name('longbing_card_user_info')
            ->alias('a')
            ->leftJoin('longbing_card_job b','b.id = a.job_id')
            ->where($dis)
            ->field('a.fans_id,a.name,a.avatar,b.name as job_name')
            ->find();
        $user = transImagesOne($user,['avatar']);
        if(empty($user['avatar'])){
            $user['avatar'] = lbUserInfoAratar($user['fans_id']);
        }
        return $user;
    }

    /**
     * User: chenniang
     * Date: 2019-10-15 11:51
     * @param $value
     * @param $data
     * @return mixed
     * descption:获取风格名字
     */
    public function getStyleTextAttr($value,$data){
        $style_model = new AdminStyle();
        return $style_model->styleTitle(['id'=>$data['style']]);
    }
    /**
     * User: chenniang
     * Date: 2019-10-15 11:57
     * @param $value
     * @param $data
     * @return mixed
     * descption:居室名字
     */
    public function getHousewearTextAttr($value,$data){
        $wear_model = new AdminHousewear();
        return $wear_model->housewearTitle(['id'=>$data['housewear']]);
    }
    /**
     * @param $value
     * @return array|mixed
     * 转换图片
     */
    public function getImgsAttr($value){
        $img = transImages([$value],['imgs'],',');
        return is_array($img[0])?$img[0]:[];
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 案例列表
     */

    public function caseList($dis,$page = 10,$dis_price=[],$dis_m=[],$sort=0){

        $sort = $this->sort($sort);

        $data = $this->where($dis)->
        where(function ($query) use ($dis_price){
            $query->whereOr($dis_price);
        })->where(function ($query) use ($dis_m){
            $query->whereOr($dis_m);
        })->status(1)
            ->order($sort)
            ->paginate($page)
            ->toArray();
        return $data;
    }

    /**
     * User: chenniang
     * Date: 2019-10-18 15:57
     * @param $sort
     * @return string
     * descption:排序
     */
    public function sort($sort){
        $data = '';
        switch ($sort){
            case 0:
                $data = 'sort desc,id desc';
                break;
            case 1:
                $data = 'iv desc,sort desc';
                break;
            case 2:
                $data = 'collect desc,sort desc';
                break;
        }
        return $data;
    }
    /**
     * User: chenniang
     * Date: 2019-10-21 16:41
     * @param $dis
     * @return mixed
     * descption:推荐案列
     */
    public function caseSelect($dis){
        $data = $this->where($dis)->status(1)->order('sort desc')->limit(5)->select()->toArray();
        return $data;
    }

    /**
     * @param $dis
     * @return AdminCate
     * 案例详情
     */
    public function caseInfo($dis){
        $data = $this->where($dis)->status(1)->find();

        if(!empty($data)){

            $data->toArray();

            $data['content1'] = getimgsV2($data['content']);

        }
        return $data;
    }

    /**
     * @param $dis
     * 案例5条
     */
    public function caseLimit($dis){
        $data = $this->where($dis)->status(1)->order('sort desc')->limit(5)->select()->toArray();
        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-30 15:02
     * @功能说明:员工自选
     */
    public function staffCaseShow($staff_id,$uniacid,$number=99999){

        $dis=[

            'a.uniacid' => $uniacid,

            'a.status'  => 1,

            'b.user_id' => $staff_id
        ];

        $id = $this->alias('a')
            ->join('longbing_card_decorate_staff_case b','a.id = b.case_id')
            ->where($dis)
            ->group('a.id')
            ->limit($number)
            ->column('a.id');

        return $id;

    }



}